### REPLICATION 
### RD & Results plots for business_mayors
### this script produces RD plots and RD results plots from main text (Figures 2-6)


rm(list=ls(all=TRUE))

library(tidyverse)
library(scales)
library(coefplot)
library(xtable)


## set working directory
setwd("")

source("replication_mayors_source.R")
source("replication_clean_descriptive_stats.R")
source("replication_clean_results.R")
source("replication_bin_data_rd_plots.R")



## Table 1

print.xtable(xtable(select(summary_stats_cities, variable_label, all, candidates, RD)),
             hline.after = NULL,
             only.contents = TRUE,
             include.rownames = FALSE,
             include.colnames = FALSE,
             add.to.row = list(pos = list(c(1, 3, 5, 7, 9, 11)), command = c("\\\\\n")),
             file = "tab_1_replication_summary_stats_cities_RR.tex")



## Figure 1
## generate figure
plot_occupation_time_mayors <- ggplot(gg_occupation_year4_mayors, aes(x = year4, y = occupation_share, linetype = occupation_cat, shape = occupation_cat)) + 
  scale_y_continuous(limits = c(0, .65), breaks = seq(0, .60, .20), labels = percent) + 
  scale_x_continuous(breaks = seq(1950, 2010, 10)) +
  geom_line() +
  xlab("Year") +
  ylab("Share") +
  theme_bw() +
  theme(legend.position = c(0.85, 0.85),
        legend.title = element_blank(),
        legend.box.background = element_rect(),
        axis.text.x  = element_text(size = 12),
        axis.text.y  = element_text(size = 12),
        strip.text.x = element_text(size = 12),
        axis.title.x = element_text(size = 16),
        axis.title.y = element_text(size = 16)) 

## return figure
plot_occupation_time_mayors

## save figure
ggsave(plot_occupation_time_mayors, filename = "fig_1_replication_plot_occupation_time_mayors.pdf", height = 10, width = 10)



## Table 2
## race & ethnicity
print.xtable(xtable(select(tab_race, race, n_mayor, pct_mayor, n_runnerup, pct_runnerup), digits = 0),
             hline.after = NULL,
             only.contents = TRUE,
             include.rownames = FALSE,
             include.colnames = FALSE,
             file = "tab_2_replication_race_tab_RR.tex")

## gender
print.xtable(xtable(select(tab_gender, gender, n_mayor, pct_mayor, n_runnerup, pct_runnerup), digits = 0),
             hline.after = NULL,
             only.contents = TRUE,
             include.rownames = FALSE,
             include.colnames = FALSE,
             file = "tab_2_replication_gender_tab_RR.tex")

## no political experience
print.xtable(xtable(select(filter(tab_experience, experience == "No Experience"),
                           experience, n_mayor, pct_mayor, n_runnerup, pct_runnerup), digits = 0),
             hline.after = NULL,
             only.contents = TRUE,
             include.rownames = FALSE,
             include.colnames = FALSE,
             file = "tab_2_replication_no_experience_tab_RR.tex")

## political experience
print.xtable(xtable(select(filter(tab_experience, experience != "No Experience"),
                           experience, n_mayor, pct_mayor, n_runnerup, pct_runnerup), digits = 0),
             hline.after = NULL,
             only.contents = TRUE,
             include.rownames = FALSE,
             include.colnames = FALSE,
             file = "tab_2_replication_experience_tab_RR.tex")


## Table 3
print.xtable(xtable(select(tab_occupation, occupation_cat, n_mayor, pct_mayor, n_runnerup, pct_runnerup), digits = 0),
             hline.after = NULL,
             only.contents = TRUE,
             include.rownames = FALSE,
             include.colnames = FALSE,
             file = "tab_3_replication_occupation_tab_RR.tex")



##  Figure 2
## generate figure
plot_size_govt <- ggplot(filter(gg_results_CCT, outcome_class == "size", specs == "CCT covs"), aes(x = coef, y = fct_rev(outcome))) +
  geom_point(size = 2.5) +
  geom_errorbarh(aes(y = outcome, xmin = coef - (-qnorm((1-0.90)/2) * robust_se), xmax = coef + (-qnorm((1-0.90)/2) * robust_se)), height = 0, size = 1) +
  geom_errorbarh(aes(y = outcome, xmin = coef - (-qnorm((1-0.95)/2) * robust_se), xmax = coef + (-qnorm((1-0.95)/2) * robust_se)), height = 0, linetype = "dashed") +
  facet_grid(outcome_type ~ ., scales = "free_y", space = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", colour = "grey") +
  scale_x_continuous(breaks = seq(-750, 750, 250), labels = dollar) +
  xlab("\n Effect of Electing a Business Executive Mayor") +
  ylab("") +
  coord_cartesian(xlim = c(-1000, 1000)) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        strip.text.y = element_text(size = 12))

## return figure
plot_size_govt

## save figure as pdf
ggsave(plot_size_govt, filename = "fig_2_replication_plot_results_size_govt_RR.pdf", height = 10, width = 10)


## Figure 3
## generate plot
plot_spending <- ggplot(filter(gg_results_CCT, outcome_class != "size", specs == "CCT covs"), aes(x = coef, y = fct_rev(outcome))) +
  geom_point(size = 2.5) +
  geom_errorbarh(aes(y = outcome, xmin = coef - (-qnorm((1-0.90)/2) * robust_se), xmax = coef + (-qnorm((1-0.90)/2) * robust_se)), height = 0, size = 1) +
  geom_errorbarh(aes(y = outcome, xmin = coef - (-qnorm((1-0.95)/2) * robust_se), xmax = coef + (-qnorm((1-0.95)/2) * robust_se)), height = 0, linetype = "dashed") +
  facet_grid(outcome_type ~ ., scales = "free_y", space = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", colour = "grey") +
  scale_x_continuous(breaks = seq(-100, 100, 50), labels = dollar) +
  xlab("\n Effect of Electing a Business Executive Mayor") +
  ylab("") +
  coord_cartesian(xlim = c(-200, 200)) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        strip.text.y = element_text(size = 12))

## return plot
plot_spending

## save plot as pdf
ggsave(plot_spending, filename = "fig_3_replication_plot_results_spending_RR.pdf", height = 10, width = 10)


## Figure 4(a)
## generate figure


plot_roads_binned_RR <- ggplot(binned_roads, aes(x = binned_margin, y = binned_roads, group = binned_margin > 0)) +
  geom_point(alpha = .5) +
  geom_smooth(method = "lm", se = FALSE) +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  scale_x_continuous(limits = c(-.25, .25)) +
  scale_y_continuous(limits = c(0, 250), breaks = seq(0, 225, 75), labels = dollar) +
  xlab("Business Executive Vote Share Margin") +
  ylab("Roads (per capita)") +
  theme_bw() +
  theme(axis.title.x  = element_text(size=16, margin = margin(t = 16)),
        axis.text.x  = element_text(size=12),
        axis.title.y  = element_text(size=16, margin = margin(r = 16)),
        axis.text.y  = element_text(size=12),
        legend.position = "none") 


## return figure
plot_roads_binned_RR

## save figure
ggsave(plot_roads_binned_RR, file = "fig_4a_replication_plot_roads_binned_RR.pdf", width = 10, height = 8)

## Figure 4(b)
## generate figure
plot_housing_binned_RR <- plot_roads_binned_RR %+% 
  binned_housing %+% aes(x = binned_margin, y = binned_housing, group = binned_margin > 0) +
  scale_y_continuous(limits = c(0, 250), breaks = seq(0, 225, 75), labels = dollar) +
  xlab("Business Executive Vote Share Margin") +
  ylab("Spending: Housing & Comm. Development (per capita)") 

## return plot
plot_housing_binned_RR

## save figure
ggsave(plot_housing_binned_RR, file = "fig_4b_replication_plot_housing_binned_RR.pdf", width = 10, height = 8)




## Figure 5(b)
## generate figure
plot_multi_year_roads <- ggplot(filter(gg_multi_year_results_CCT, specs == "CCT covs", (outcome == "Roads" | outcome == "Total Infrastructure & \n Amenities")), 
                                aes(x = lead_years, y = coef, shape = outcome)) +
  geom_point(size = 4, position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "grey") +
  geom_errorbar(aes(ymin = coef - (-qnorm((1-0.90)/2) * robust_se), ymax = coef + (-qnorm((1-0.90)/2) * robust_se)), width = 0, size = 1, position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = coef - (-qnorm((1-0.95)/2) * robust_se), ymax = coef + (-qnorm((1-0.95)/2) * robust_se)), width = 0, linetype = "dashed", position = position_dodge(width = 0.5)) +
  scale_x_discrete(breaks = c("_lead1", "_lead2", "_lead3", "_lead4"), labels = c("1", "2", "3", "4")) +
  scale_y_continuous(limits = c(-200, 200), labels = dollar) +
  xlab("\n Years After Election") +
  ylab("Effect") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 16),
        axis.title.y = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        strip.text.y = element_text(size = 12),
        legend.position = "bottom", 
        legend.title = element_blank())

## return figure
plot_multi_year_roads

## save figure
ggsave(plot_multi_year_roads, filename = "fig_5b_replication_plot_multi_year_roads_RR.pdf", width = 8, height = 6)

## Figure 5(c)
## generate figure  
plot_multi_year_housing <- plot_multi_year_roads %+%
  filter(gg_multi_year_results_CCT, specs == "CCT covs", (outcome == "Housing & Community \nDevelopment" | outcome == "Total Redistributive \n Programs")) 

## return figure
plot_multi_year_housing

## save figure as pdf
ggsave(plot_multi_year_housing, filename = "fig_5c_replication_plot_multi_year_housing_RR.pdf", width = 8, height = 6)

## Figure 5(a)
## generate figure  
plot_multi_year_own_revs <- plot_multi_year_roads %+%
  filter(gg_multi_year_results_CCT, specs == "CCT covs", (outcome == "Own-source \n Revenue" | outcome == "Charges & Misc. \n Revenue")) +
  scale_y_continuous(limits = c(-50, 450), labels = dollar) 

## return figure
plot_multi_year_own_revs

## save figure as pdf  
ggsave(plot_multi_year_own_revs, filename = "fig_5a_replication_plot_multi_year_own_revs_RR.pdf", width = 8, height = 6)




## Figure 6(a)
## generate figure  
plot_size_govt_republican <- ggplot(filter(gg_republican, outcome_class == "size"),
                                    aes(x = covs_coef, y = fct_rev(outcome), group = candidate_type)) +
  geom_point(aes(x = covs_coef, y = fct_rev(outcome), color = candidate_type, shape = candidate_type), position = position_dodgev(height = .5), size = 2.5) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.90)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.90)/2) * covs_robust_se), 
                     color = candidate_type), size = 1, position = position_dodgev(height = .5), height = 0) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.95)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.95)/2) * covs_robust_se), 
                     color = candidate_type), position = position_dodgev(height = .5), height = 0, linetype = "dashed") +
  scale_color_manual(values = c("black", "red", "grey")) +
  facet_grid(outcome_type ~ ., scales = "free_y", space = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", colour = "grey") +
  scale_x_continuous(breaks = seq(-750, 750, 250), labels = dollar) +
  xlab("\n Effect of Electing a \nRepublican") +
  ylab("") +
  coord_cartesian(xlim = c(-1000, 1000)) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 12),
        axis.text.x = element_text(size = 10),
        axis.text.y = element_text(size = 12),
        strip.text.y = element_text(size = 8),
        legend.position = "bottom",
        legend.title = element_blank())


## return figure
plot_size_govt_republican


## save figure
ggsave(plot_size_govt_republican, filename = "fig_6a_replication_plot_results_size_govt_party_republican_RR.pdf", height = 8, width = 6)



## Figure 6(b)
## generate figure  
plot_spending_republican <- ggplot(filter(gg_republican, outcome_class == "spending" | outcome_class == "spending category"),
                                   aes(x = covs_coef, y = fct_rev(outcome), group = candidate_type))  +
  geom_point(aes(x = covs_coef, y = fct_rev(outcome), color = candidate_type, shape = candidate_type), position = position_dodgev(height = .5), size = 2.5) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.90)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.90)/2) * covs_robust_se), 
                     color = candidate_type), size = 1, position = position_dodgev(height = .5), height = 0) +
  geom_errorbarh(aes(xmin = covs_coef - (-qnorm((1-0.95)/2) * covs_robust_se), xmax = covs_coef + (-qnorm((1-0.95)/2) * covs_robust_se), 
                     color = candidate_type), position = position_dodgev(height = .5), height = 0, linetype = "dashed") +
  scale_color_manual(values = c("black", "red", "grey")) +
  facet_grid(outcome_type ~ ., scales = "free_y", space = "free_y") +
  geom_vline(xintercept = 0, linetype = "dashed", colour = "grey") +
  scale_x_continuous(breaks = seq(-100, 100, 50), labels = dollar) +
  xlab("\n Effect of Electing a \nRepublican") +
  ylab("") +
  coord_cartesian(xlim = c(-200, 200)) +
  theme_bw() +
  theme(axis.title.x = element_text(size = 12),
        axis.text.x = element_text(size = 10),
        axis.text.y = element_text(size = 12),
        strip.text.y = element_text(size = 10),
        legend.position = "bottom",
        legend.title = element_blank())


## return figure
plot_spending_republican

## save figure
ggsave(plot_spending_republican, filename = "fig_6b_replication_plot_results_spending_party_republican_RR.pdf", height = 8, width = 6)


